using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace UserNode
{
    public partial class Form1 : Form
    {
        Image m_bgImage = null;
        Graphics m_bgImageGraphics = null;
        List<Node> m_Nodes = new List<Node>();
        Pen m_DragPen = new Pen(Color.Black);
        List<Button> m_JobButtons = new List<Button>();

        public Form1()
        {
            InitializeComponent();

            m_bgImage = new Bitmap(gamePictureBox.Width, gamePictureBox.Height);
            m_bgImageGraphics = Graphics.FromImage(m_bgImage);

            List<string> nodes = new List<string>();
            for (int i = 0; i < 21; i++)
            {
                nodes.Add(String.Format("node{0}", i + 1));
            }
            CreateNodes(nodes);

            gamePictureBoxUpdateTimer.Enabled = true;

            for (int i = 0; i < 5; i++)
            {
                Button b = new Button();
                b.Text = String.Format("button {0}\nmultiline?", i);
                AddJobButton(b);
            }
        }

        private void CreateNodes(List<string> nodeList)
        {
            int n = 1;
            while (n < nodeList.Count / n)
                n++;
            for (float y = 0; y < n; y++)
            {
                for (float x = 0; x < n && y * n + x < nodeList.Count; x++)
                {
                    m_Nodes.Add(new Node(x / n + 0.15f/n, y / n + 0.15f /n, 0.7f / n, x < (n / 2)));
                }
            }
        }

        void GameUpdate()
        {
            DateTime now = DateTime.Now;

            foreach (Node node in m_Nodes)
            {
                node.Update(now, m_bgImage.Size, m_MouseDragRectangle);
            }
        }

        void DrawGamePicture()
        {
            m_bgImageGraphics.Clear(Color.White);
            System.Drawing.Size size = new Size(m_bgImage.Width, m_bgImage.Height);

            foreach (Node node in m_Nodes)
            {
                node.Draw(m_bgImageGraphics, size);
            }

            if (m_MouseDragging)
            {
                m_bgImageGraphics.DrawRectangle(m_DragPen, m_MouseDragRectangle);
            }
        }

        private void gamePictureBoxUpdateTimer_Tick(object sender, EventArgs e)
        {
            GameUpdate();
            DrawGamePicture();
            gamePictureBox.Image = m_bgImage;
            gamePictureBox.Refresh();
        }

        private void gamePictureBox_SizeChanged(object sender, EventArgs e)
        {
            m_bgImage = new Bitmap(gamePictureBox.Width, gamePictureBox.Height);
            m_bgImageGraphics = Graphics.FromImage(m_bgImage);
        }

        Point m_MouseDownPoint = new Point(-1, -1);
        bool m_MouseDragging = false;
        Point m_NowMousePoint = new Point(-1, -1);
        Rectangle m_MouseDragRectangle = new Rectangle(-1, -1, 0, 0);
        private void gamePictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            m_MouseDownPoint = e.Location;
            m_MouseDragging = true;
            m_NowMousePoint = e.Location;
        }

        private void gamePictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            gamePictureBox_MouseMove(sender, e);
            if (m_MouseDragging)
            {

            }
            m_MouseDownPoint = new Point(-1, -1);
            m_MouseDragging = false;
            m_NowMousePoint = new Point(-1, -1);
            m_MouseDragRectangle = new Rectangle(-1, -1, 0, 0);
        }

        private void gamePictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            m_NowMousePoint = e.Location;

            if (m_MouseDragging)
            {
                if (m_MouseDownPoint.X < m_NowMousePoint.X)
                {
                    m_MouseDragRectangle.X = m_MouseDownPoint.X;
                    m_MouseDragRectangle.Width = m_NowMousePoint.X - m_MouseDownPoint.X;
                }
                else
                {
                    m_MouseDragRectangle.X = m_NowMousePoint.X;
                    m_MouseDragRectangle.Width = m_MouseDownPoint.X - m_NowMousePoint.X;
                }

                if (m_MouseDownPoint.Y < m_NowMousePoint.Y)
                {
                    m_MouseDragRectangle.Y = m_MouseDownPoint.Y;
                    m_MouseDragRectangle.Height = m_NowMousePoint.Y - m_MouseDownPoint.Y;
                }
                else
                {
                    m_MouseDragRectangle.Y = m_NowMousePoint.Y;
                    m_MouseDragRectangle.Height = m_MouseDownPoint.Y - m_NowMousePoint.Y;
                }
            }
        }

        private void RescaleButtons()
        {
            int width = (jobButtonPanel.Width - 5) / m_JobButtons.Count;
            int x = 2;
            jobButtonPanel.SuspendLayout();
            if (width < 3)
                width = 3;
            foreach (Button b in m_JobButtons)
            {
                b.Left = x;
                x += width;
                b.Width = width - 2;
            }
            jobButtonPanel.ResumeLayout();
            foreach (Button b in m_JobButtons)
            {
                b.Update();
            }
            jobButtonPanel.Update();
        }
        private void AddJobButton(Button button)
        {
            if (button == null)
                return;
            m_JobButtons.Add(button);
            button.AutoSize = true;
            
            button.AutoSizeMode = AutoSizeMode.GrowOnly;
            button.Anchor = (AnchorStyles)(/* AnchorStyles.Left
                | AnchorStyles.Right | */AnchorStyles.Top | AnchorStyles.Bottom);
            button.Click += new EventHandler(JobButton_Click);
            jobButtonPanel.Controls.Add(button);
            RescaleButtons();
        }

        void JobButton_Click(object sender, EventArgs e)
        {
            RemoveButton((Button)sender);
        }

        private void RemoveButton(Button button)
        {
            if (m_JobButtons.Remove(button) == true)
            {
                jobButtonPanel.Controls.Remove(button);
                RescaleButtons();
            }
        }

        private void jobButtonPanel_Resize(object sender, EventArgs e)
        {
            RescaleButtons();
        }

    }
}